package com.soushin.chinachess.module;

import java.util.ArrayList;
import java.util.List;

import com.soushin.chinachess.R;
import com.soushin.chinachess.base.BaseActivity;
import com.soushin.chinachess.utils.SharedUtils;
import com.soushin.chinachess.widget.ListDialog;
import com.soushin.chinachess.widget.ListDialog.OnSelectListener;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

/**
 * 设置
 * 
 * @author Administrator
 *
 */
public class SettingsActivity extends BaseActivity
		implements OnClickListener, OnCheckedChangeListener, OnSelectListener {
	private RelativeLayout rltComprimise, rltEnable, rltStyle;
	private Switch switchSound, switchFirst;
	private TextView tvCompromise, tvEnable, tvStyle;
	private ListDialog lDialog;
	private List<String> itemList1,itemList2,itemList3;
	private int clickType;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		initData();
	}

	private void initData() {
		// TODO Auto-generated method stub
		setBack(true);
		setTitle("游戏设置");
		rltComprimise = (RelativeLayout) findViewById(R.id.rlt_comprimise);
		rltEnable = (RelativeLayout) findViewById(R.id.rlt_enable);
		rltStyle = (RelativeLayout) findViewById(R.id.rlt_style);
		switchSound = (Switch) findViewById(R.id.switch_sound);
		switchFirst = (Switch) findViewById(R.id.switch_first);
		tvCompromise = ((TextView) findViewById(R.id.tv_compromise));
		tvEnable = ((TextView) findViewById(R.id.tv_enable));
		tvStyle = ((TextView) findViewById(R.id.tv_style));
		switchFirst.setOnCheckedChangeListener(this);
		switchSound.setOnCheckedChangeListener(this);
		rltComprimise.setOnClickListener(this);
		rltEnable.setOnClickListener(this);
		rltStyle.setOnClickListener(this);
		
		itemList1 = new ArrayList<String>();
		itemList2 = new ArrayList<String>();
		itemList3 = new ArrayList<String>();
		itemList1.add("不让子");
		itemList1.add("让左马");
		itemList1.add("让双马");
		itemList1.add("让九子");
		itemList2.add("入门");
		itemList2.add("业余");
		itemList2.add("专业");
		itemList2.add("大师");
		itemList2.add("特级大师");
		itemList3.add("卡通");
		itemList3.add("木纹");
		switchSound.setChecked(SharedUtils.getSwitch_sound()==1);// 音效
		switchFirst.setChecked(SharedUtils.getSwitch_computer_first()==1);// 电脑先走
		
		clickType=0;
		int mHandicapIndex = SharedUtils.getHandicap();// 先走让子
		onSelect(mHandicapIndex, itemList1.get(mHandicapIndex));
		
		clickType=1;
		int mAILevel = SharedUtils.getComputer_power();// 电脑棋力
		onSelect(mAILevel, itemList2.get(mAILevel));
		
		clickType=2;
		int mPieceStyle = SharedUtils.getList_chess_style();// 界面样式
		onSelect(mPieceStyle, itemList3.get(mPieceStyle));
		clickType=0;//还原点击类型

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		if (lDialog != null && lDialog.isShowing()) {
			lDialog.dismiss();
		}
		super.onDestroy();
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		Log.e("点击了",isChecked+"");
		switch (buttonView.getId()) {
		case R.id.switch_sound:
			SharedUtils.setSwitch_sound(isChecked?1:0);
			break;
		case R.id.switch_first:
			SharedUtils.setSwitch_computer_first(isChecked?1:0);
			break;
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		switch (v.getId()) {
		case R.id.rlt_comprimise:// 先走让子
			clickType = 0;
			showListDialog(itemList1);
			break;
		case R.id.rlt_enable:// 电脑棋力
			clickType = 1;
			showListDialog(itemList2);
			break;
		case R.id.rlt_style:// 棋子样式
			clickType = 2;
			showListDialog(itemList3);
			break;
		}

	}

	private void showListDialog(List<String> items) {
		// TODO Auto-generated method stub
		lDialog = new ListDialog(getActivity(), items);
		lDialog.setOnSelectListener(this);// 实现item点击接口
		lDialog.show();
	}

	@Override
	public void onSelect(int position, String item) {
		// TODO Auto-generated method stub
		switch (clickType) {
		case 0:
			SharedUtils.setHandicap(position);
			setTvCompromise("先走一方让子(重开局生效): " + item);
			break;
		case 1:
			SharedUtils.setComputer_power(position);
			setTvEnable("电脑棋力的级别: " + item);
			break;
		case 2:
			SharedUtils.setList_chess_style(position);
			setTvStyle("棋子在棋盘中的样式: " + item);
			break;
		default:
			break;
		}
	}

	public void setTvCompromise(String compromise) {
		this.tvCompromise.setText(compromise);
	}

	public void setTvEnable(String enable) {
		this.tvEnable.setText(enable);
	}

	public void setTvStyle(String style) {
		this.tvStyle.setText(style);
	}

}
